package com.medical.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.system.converter.DictConverter;
import com.medical.system.mapper.SysDictMapper;
import com.medical.system.model.entity.SysDict;
import com.medical.system.model.form.DictForm;
import com.medical.system.model.vo.DictVO;
import com.medical.system.service.SysDictService;
import com.medical.web.model.Option;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private final DictConverter dictConverter;

    @Override
    public List<DictVO> getDictList(String typeCode) {
        List<SysDict> list =  list(new LambdaQueryWrapper<SysDict>()
                .eq(StrUtil.isNotBlank(typeCode), SysDict::getTypeCode, typeCode)
                .select(SysDict::getId,
                        SysDict::getName,
                        SysDict::getValue,
                        SysDict::getTypeCode,
                        SysDict::getSort,
                        SysDict::getStatus)
                .orderByAsc(SysDict::getSort)
        );
        return dictConverter.entitys2VOs(list);
    }

    @Override
    public DictForm getDictForm(Long id) {
        SysDict sysDict = this.getOne(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getId, id)
                .select(
                        SysDict::getId,
                        SysDict::getTypeCode,
                        SysDict::getName,
                        SysDict::getValue,
                        SysDict::getStatus,
                        SysDict::getSort,
                        SysDict::getRemark));
        Assert.isTrue(sysDict != null, "字典数据项不存在");
        DictForm dictForm = dictConverter.entity2Form(sysDict);
        return dictForm;
    }

    @Override
    public boolean saveDict(DictForm dicForm) {
        SysDict sysDict = dictConverter.form2Entity(dicForm);
        return save(sysDict);
    }

    @Override
    public boolean updateDict(Long id, DictForm dicForm) {
        SysDict sysDict = dictConverter.form2Entity(dicForm);
        return updateById(sysDict);
    }

    @Override
    public boolean deleteDict(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除数据为空");
        List<Long> ids = Arrays.asList(idsStr.split(","))
                .stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());
        return removeByIds(ids);
    }

    @Override
    public List<Option> listDictOptions(String typeCode) {
        List<SysDict> sysDictList =  list(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getTypeCode, typeCode)
                .select(SysDict::getName, SysDict::getValue)
        );
        return CollectionUtil.emptyIfNull(sysDictList)
                .stream()
                .map(item -> new Option(item.getValue(), item.getName()))
                .collect(Collectors.toList());
    }
}
